﻿using AyaWo.Entity;
using DbEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class SpiderContentService : BaseService
    {

        #region 插入获取数据到数据表
        /// <summary>
        /// 插入获取的数据到数据表中
        /// </summary>
        /// <param name="obj"></param>
        public static void InsertContent(SpiderContent obj)
        {
            new SpiderContentService().InsertContents(obj);
        }
        public void InsertContents(SpiderContent obj)
        {
            string sqlstr = "INSERT into spidercontent(ID,Link,Title,BuildTime,Source,Autos,Content,CreateTime) VALUES(@ID,@Link,@Title,@BuildTime,@Source,@Autos,@Content,@CreateTime)";
            Db.ExecuteBySql(new StringBuilder(sqlstr), obj);
        }
        #endregion

        #region MyRegion
        /// <summary>
        /// 判断抓取的数据连接和标题是否之前已经存在数据库中了
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Link"></param>
        /// <returns></returns>
        public static bool CanInsert(string Title, string Link)
        {
            return new SpiderContentService().CanInserts(Title, Link);
        }

        public bool CanInserts(string Title, string Link)
        {
            try
            {
                var q = Db.GetDataListBySqL<SpiderContent>(new StringBuilder("select ID from spidercontent t where t.Link like @Link"), new {Link = "%"+Link+"%" });
                if (q == null || q.Count() == 0)
                    return true;
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region 获取在还没有被关键字搜过过的  且 关键字有设置过的数据列表
        /// <summary>
        /// 获取在还没有被关键字搜过过的  且 关键字有设置过的数据列表  
        /// </summary>
        /// <returns></returns>
        public List<SpiderContent> GetListSp()
        {
            string sqlStr = "select t.ID,t.key1,t.key2,t.key3,t.key4,t.key5 from spidercontent t WHERE (t.HaSo!=1 or t.HaSo is null ) and (t.key1 is NOT NULL OR t.key2 IS NOT NULL OR t.key3 IS not NULL OR t.key4 IS NOT NULL OR t.key5 IS NOT NULL)";
            return Db.GetDataListBySqL<SpiderContent>(new StringBuilder(sqlStr));
        }
        #endregion


        public static List<SpiderContent> GetListFenCi()
        {
          return new SpiderContentService().GetListFenCis();
        }
        public List<SpiderContent> GetListFenCis()
        {
            string sqlStr = "select t.ID,t.Title,t.key1,t.key2,t.key3,t.key4,t.key5 from spidercontent t WHERE (t.HaSo!=1 or t.HaSo is null )";
            return Db.GetDataListBySqL<SpiderContent>(new StringBuilder(sqlStr));
        }

        #region 更新键值对

        #region Upk1v1
        /// <summary>
        /// 更新键值1
        /// </summary>
        /// <param name="kv"></param>
        /// <returns></returns>
        public int Upk1v1(string keyID,KeyValueModel kv)
        {
            var sqlStr = "UPDATE spidercontent SET getKey1=@getKey1, getValue1=@getValue1 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { getKey1 = kv.KeyName, getValue1 = kv.value, ID = keyID });
        }
        #endregion

        #region Upk2v2
        /// <summary>
        /// 更新键值2
        /// </summary>
        /// <param name="kv"></param>
        /// <returns></returns>
        public int Upk2v2(string keyID, KeyValueModel kv)
        {
            var sqlStr = "UPDATE spidercontent SET getKey2=@getKey2, getValue2=@getValue2 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { getKey2 = kv.KeyName, getValue2 = kv.value, ID = keyID });
        }
        #endregion

        #region Upk3v3
        /// <summary>
        /// 更新键值3
        /// </summary>
        /// <param name="kv"></param>
        /// <returns></returns>
        public int Upk3v3(string keyID, KeyValueModel kv)
        {
            var sqlStr = "UPDATE spidercontent SET getKey3=@getKey3, getValue3=@getValue3 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { getKey3 = kv.KeyName, getValue3 = kv.value, ID = keyID });
        }
        #endregion

        #region Upk4v4
        /// <summary>
        /// 更新键值4
        /// </summary>
        /// <param name="kv"></param>
        /// <returns></returns>
        public int Upk4v4(string keyID, KeyValueModel kv)
        {
            var sqlStr = "UPDATE spidercontent SET getKey4=@getKey4, getValue4=@getValue4 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { getKey4 = kv.KeyName, getValue4 = kv.value ,ID=keyID});
        }
        #endregion

        #region Upk5v5
        /// <summary>
        /// 更新键值5
        /// </summary>
        /// <param name="kv"></param>
        /// <returns></returns>
        public int Upk5v5(string keyID, KeyValueModel kv)
        {
            var sqlStr = "UPDATE spidercontent SET getKey5=@getKey5, getValue5=@getValue5 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { getKey5 = kv.KeyName, getValue5 = kv.value, ID = keyID });
        }
        #endregion

        #endregion

        #region 更新搜索状态
        /// <summary>
        /// 更新搜索状态
        /// </summary>
        /// <param name="keyID"></param>
        /// <returns></returns>
        public int UpHaso(string keyID, string key1, string key2, string key3, string key4, string key5)
        {
            var sqlStr = "UPDATE spidercontent SET HaSo=1,key1=@key1,key2=@key2,key3=@key3,key4=@key4,key5=@key5 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { ID = keyID, key1 = key1, key2 = key2, key3 = key3, key4 = key4, key5 = key5 });
        }
        #endregion

        #region 获取可以同步的数据列表
        /// <summary>
        /// 获取可以同步的数据列表
        /// </summary>
        /// <returns></returns>
        public List<SpiderContent> GetListCanTonBu()
        {
            string sqlStr = "SELECT * from spidercontent t where t.HaSo=1 AND (t.HasTonbu!=1 or t.HasTonbu is null) AND( t.getValue1!=0 OR t.getValue2!=0 OR t.getValue3!=0 OR t.getValue4!=0 OR t.getValue5!=0) ORDER BY t.CreateTime DESC";
            return Db.GetDataListBySqL<SpiderContent>(new StringBuilder(sqlStr));
        }
        #endregion

        #region 更新更新后的标题并标记已经同步标志
        /// <summary>
        /// 更新更新后的标题并标记已经同步标志
        /// </summary>
        /// <param name="keyID"></param>
        /// <param name="titleContent"></param>
        /// <returns></returns>
        public int UpAfterTitle(string keyID,string titleContent)
        {
            var sqlStr = "UPDATE spidercontent SET AfuterTitle=@AfuterTitle,HasTonbu=1 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), new { ID = keyID, AfuterTitle= titleContent });
        }
        #endregion

        #region 获取需要设置关键字的列表
        /// <summary>
        ///  获取需要设置关键字的列表
        /// </summary>
        /// <returns></returns>
        public List<SPFormModel> ShoudSoList()
        {
            string sqlStr = "SELECT t.ID,t.Title,t.key1,t.key2,t.key3,t.key4,t.key5 from spidercontent t WHERE (t.HaSo!=1 OR t.HaSo IS NULL)";
            return Db.GetDataListBySqL<SPFormModel>(new StringBuilder(sqlStr));
        }
        /// <summary>
        /// 获取需要设置关键字的列表
        /// </summary>
        /// <returns></returns>
        public static List<SPFormModel> ShoudSoLists()
        {
           return new SpiderContentService().ShoudSoList();
        }
        #endregion


        #region 更新键值1,2,3,4,5
        /// <summary>
        /// 更新关键词数据
        /// </summary>
        /// <param name="spfm"></param>
        /// <returns></returns>
        public int UpKey12345(SPFormModel spfm)
        {
            string sqlStr = "UPDATE spidercontent SET key1=@key1,key2=@key2,key3=@key3,key4=@key4,key5=@key5 WHERE ID=@ID";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), spfm);
        }
        /// <summary>
        /// 更新关键词数据
        /// </summary>
        /// <param name="spfm"></param>
        /// <returns></returns>
        public static int UpKey12345s(SPFormModel spfm)
        {
            return new SpiderContentService().UpKey12345(spfm);
        }
        #endregion
    }
}
